package com.ruoyi.web.controller.resturant;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BizException;
import com.ruoyi.resturant.domain.ResBanner;
import com.ruoyi.resturant.service.IResBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(tags = "轮播图管理")
@RestController
@RequestMapping("/api/resturant/banner/纪人豪/")
public class ResBannerController {


    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IResBannerService IResBannerService;

    @ApiOperation("查询所有轮播图 23201012809纪人豪")
    @ApiImplicitParam(name = "name", value = "轮播图名称", dataType = "String",
            dataTypeClass = String.class)
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "name", required = false)
                               String bannerName) {
        List<ResBanner> list = IResBannerService.selectBannerByParam(bannerName);
                     //
        return AjaxResult.success(list);
    }

    /**
     * 获取轮播图详细信息
     */
    @ApiOperation( "获取轮播图详细信息 23201012809纪人豪")
    @ApiImplicitParam(name = "id", value = "轮播图ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        // 1 根据轮播图ID从Redis中取得轮播图JSON字符串
        ResBanner resBanner = IResBannerService.selectBannerById(id);
        if (resBanner == null) {
            throw new BizException(404, "此轮播图不存在");
        }//
        //把轮播图对象banner存到Redis中
        String str = JSON.toJSONString(resBanner);

        redisCache.setCacheObject(String.valueOf(id), str, 1, TimeUnit.HOURS);

        //返回轮播图对象
        return AjaxResult.success(resBanner);
    }

    /**
     * 新增
     */
    @Log(title = "轮播图管理", businessType = BusinessType.INSERT)
    @ApiOperation("新增轮播图 23201012809纪人豪")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ResBanner resBanner) {
        return AjaxResult.success(IResBannerService.insertBanner(resBanner));
    }

    /**
     * 修改轮播图
     */
    @Log(title = "轮播图管理", businessType = BusinessType.UPDATE)
    @ApiOperation("修改轮播图 23201012809纪人豪")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ResBanner resBanner) {

        // 1. 先从数据库查询轮播图是否存在
        ResBanner resBannerDb = IResBannerService.selectBannerById(resBanner.getId());
        if (resBannerDb == null) {
            throw new BizException(404, "此轮播图不存在");
        }

        // 2. 将DTO中的信息更新到bannerDb对象（或者根据您的业务逻辑来处理）
        // 例如：bannerDb.setName(bannerUpdateDTO.getName());

        // 3. 调用服务层来更新轮播图
        int num = IResBannerService.updateBanner(resBanner);

        return AjaxResult.success(num);
    }

    /**
     * 删除轮播图
     */
    @Log(title = "轮播图管理", businessType = BusinessType.DELETE)
    @ApiOperation("删除轮播图 23201012809纪人豪")
    @ApiImplicitParam(name = "id", value = "轮播图ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(IResBannerService.deleteBannerById(id));
    }

}



